package com.lej.login.countryCode

import android.content.Intent
import androidx.core.view.updatePadding
import androidx.core.widget.doOnTextChanged
import com.alibaba.android.arouter.facade.annotation.Route
import com.lej.core.widget.letter.LetterIndexView
import com.lej.core.base.BaseActivity
import com.lej.core.base.statusHeight
import com.lej.core.common.RESULT_DATA
import com.lej.core.router.CRouter
import com.lej.core.utils.extention.*
import com.lej.core.widget.drawable.CustomDrawable
import com.lej.login.BR
import com.lej.login.R
import com.lej.login.databinding.ActivityCountryCodeSelectBinding
import com.lej.login.databinding.ItemCountryCodeSelectBinding
import com.qmuiteam.qmui.kotlin.onClick
import com.qmuiteam.qmui.util.QMUIDrawableHelper
import com.qmuiteam.qmui.util.QMUIStatusBarHelper


@Route(path = CRouter.countryCodeSelect)
class CountryCodeSelectActivity :
    BaseActivity<ActivityCountryCodeSelectBinding, CountryCodeSelectViewModel>() {
    //国家列表
    private val countryCodeList by lazy { resources.getStringArray(R.array.countryCode).toList() }

    private var showList: List<String> = ArrayList()
    private lateinit var adapter: QuickAdapter<String>

    override fun getLayout() = R.layout.activity_country_code_select

    override fun initVariableId() = BR.vm

    override fun initData() {
        QMUIStatusBarHelper.translucent(this)
        QMUIStatusBarHelper.setStatusBarLightMode(this)
        binding.flTitle.updatePadding(top = statusHeight)
        binding.ivBack.onClick { onBackPressed() }

        binding.etSearchInput.background =
            CustomDrawable(cornerRadius = 20, solidColor = R.color.themeBackground)
        binding.etSearchInput.setIconResource(R.drawable.ic_vector_search)

        adapter = binding.rlvSelectCountryList
            .generateQ(R.layout.item_country_code_select) { h, item ->
                h.getBind<ItemCountryCodeSelectBinding>()?.apply {
                    item?.split(" ")?.let { list ->
                        tag = list[0]
                        country = list.getOrElse(0) { "" }
                        countryCode = list.getOrElse(1) { "" }
                        root.onClick {
                            list.getOrNull(1)?.let { country ->
                                setResult(RESULT_OK, Intent().apply { putExtra(RESULT_DATA, country) })
                                finish()
                            }
                        }
                    }
                }
            }
        showList = countryCodeList
        adapter.setList(showList)
    }

    override fun initViewObservable() {
        binding.etSearchInput.doOnTextChanged { text, _, _, _ ->
            filterList(text)
        }
        binding.livIndexView.setOnTouchingLetterChangedListener(object :
            LetterIndexView.OnTouchingLetterChangedListener {
            override fun onHit(letter: String?) {
                var position = -1
                if ("↑" == letter) {
                    position = 0
                } else {
                    for (index in showList.indices) {
                        if (showList[index] == letter) {
                            position = index
                            break
                        }
                    }
                }
                if (position < 0) {
                    return
                }
                binding.rlvSelectCountryList.scrollToPosition(position)
            }

            override fun onCancel() {
            }
        })
    }

    private fun filterList(content: CharSequence?) {
        showList = countryCodeList.searchOfList(content) { this }
        adapter.setList(showList)
    }
}











